<?php
/**
 * 促销活动
 * 张名尊 20140512
 */ 

class promotionAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();  
        $this->_mod = D('promotion');
    }
    
    public function _before_index() {  

    
       // $this->readyData();
       
      
    }  
    
    
    public function show(){
 		$id = I('id');
		
		$field = array(
			'p.*',
			//'s.name' => 'storeName',
            //'r.name'=>'microAreasName',
			//'c.name'=>'cityName',
		);
		
		$main = D(MODULE_NAME)->field($field)->alias('p')
				->join(C('DB_PREFIX')."store AS s ON s.id = p.storeId")
				->join(C('DB_PREFIX')."region AS r ON r.id = s.regionId")
				->join(C('DB_PREFIX')."region AS c ON c.id = r.pid")
				->where(array('p.id'=>$id))->find();
		$main['forecastingRatio'] = $main['budget']/(1200*$main['forecastingSales']);
        $main['forecastingRatio'] = number_format($main['forecastingRatio'],'2','.','').'%';
        if($main['actualSales']>0){
    		$main['actualRatio'] = $main['cost']/(1200*$main['actualSales']);
          
            $main['actualRatio'] = number_format($main['actualRatio'],'2','.','').'%';
        }
                
		$materials = D('PromotionMaterials')->where(array('pid'=>$id))->select();
		$gift = D('PromotionGift')->where(array('pid'=>$id))->select();
		
		$this->assign('materials', $materials);
		$this->assign('gift', $gift);
		$this->assign('main', $main);
		
		$this->display();       
    }
  
   protected function _search() {
        $map = array();
        
        ($theme = $this->_request('theme', 'trim')) && $map['theme'] = array('like', '%'.$theme.'%');
        ($region = $this->_request('region', 'trim')) && $map['region'] = array('like', '%'.$region.'%');
        ($storeName = $this->_request('storeName', 'trim')) && $map['storeName'] = array('like', '%'.$storeName.'%');

        $this->assign('search', array(
            'theme'=>$theme,
            
            'storeName' => $storeName,
            'region' => $region,

            
        ));
        return $map;
    }
    
    
    
    
   /**
     * 活动结果录入页面
     * 
     * 以后改成 直接请求api 里面的 action  需要解决的问题是账号登录问题。
     */ 
    public function inputResult(){
        $id = I('id');
		$materials = D('PromotionMaterials')->where(array('pid'=>$id))->select();
		$gift = D('PromotionGift')->where(array('pid'=>$id))->select();
        $main = D(MODULE_NAME)->where(array('id'=>$id))->find();
        $this->assign('main', $main);
		$this->assign('materials', $materials);
		$this->assign('gift', $gift);        
        $this->assign('id',$id);
        $this->display();
    }
    
    public function actInputResult(){
        
        $data = D('promotion')->create($_POST);      
       
        $mater = $gift = array();
        foreach($_POST['mater'] as $k=>$v){
     
                $mater[$k] = $v;
                $mater[$k]['inputUserId'] = $this->user['id'];
                $mater[$k]['inputTime'] = time();
                if(isset($mater[$k]['materialsUseNum']) && isset($mater[$k]['materialsSign'])){
                    $mater[$k]['materialsUsePrice'] = $mater[$k]['materialsUseNum'] * $mater[$k]['materialsSign'];
                }                   
       
        }
        foreach($_POST['gift'] as $k=>$v){
            $gift[$k] = $v;
            $gift[$k]['inputUserId'] = $this->user['id'];
            $gift[$k]['inputTime'] = time();
            if(isset($gift[$k]['giftSign']) && isset($gift[$k]['giftUseNum'])){
                $gift[$k]['giftUsePrice'] = $gift[$k]['giftSign'] * $gift[$k]['giftUseNum'];
            }                 
        }
       // print_r($_POST);
        $data['cost'] = 0;
        foreach($mater as $kk=>$vv){
            $data['cost'] += $vv['materialsUsePrice'];
        }
        foreach($gift as $kk=>$vv){
            $data['cost'] += $vv['giftUsePrice'];
        }
        $data['cost'] = array('exp','salerTotalPay+'.$data['cost']); 
        $res = D('promotion')->where(array('id'=>$data['id']))->save($data);
        //D('promotion')->getLastSql();
        if($res){
            foreach($gift as $k=>$v){
                D('promotion_gift')->where(array('id'=>$k))->save($v);
                //echo D('promotion_gift')->getLastSql();
            }
            foreach($mater as $k=>$v){
                D('promotion_materials')->where(array('id'=>$k))->save($v);
                //echo D('promotion_materials')->getLastSql();
            }
        }
        redirect('index.php?g=api&m=promotion&a=detail&id='.$data['id'], 0);    
    }    
    
   
    
    
}

?>